# File: setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import glob
import os.path

"""
Steps of this script:
1. Define metadata for setup().
2. Construct paths and file lists.
3. Define modules for different platforms.
4. Call setup()
"""

#-------------------------------------------------------------------------------
# 1. Define metadata of the package.
#-------------------------------------------------------------------------------
PKG_NAME = 'pynasacdf'
VERSION  = '0.1.0'
AUTHOR   = 'Jianbao Tao'
JBT_EMAIL = '%s@%s.com' % ('jianbao.tao', 'gmail')
PROJECT_URL = 'https://code.google.com/p/python-nasa-cdf-tools/'

PKG_LIST  = [PKG_NAME,
            ]

#-------------------------------------------------------------------------------
# 2. Construct directories.
#-------------------------------------------------------------------------------
# proj_dir: The directory of setup.py
proj_dir    = os.path.dirname(os.path.abspath(__file__))
# cdf_dist: Directory of the NASA CDF C library.
cdf_dist    = os.path.join(proj_dir, 'cdf34_1-dist')

# Dirs: Mac version of CDF
cdf_macosx  = os.path.join(cdf_dist, 'macosx')
cdf_macosx_inc = os.path.join(cdf_macosx, 'include') 
macosx_libcdf_a = os.path.join(cdf_macosx, 'lib', 'libcdf.a')

# Dirs: Linux version of CDF
cdf_linux     = os.path.join(cdf_dist, 'linux')
cdf_linux_lib = os.path.join(cdf_linux, 'src', 'lib') 
cdf_linux_inc = os.path.join(cdf_linux, 'src', 'include') 

# Linux C source files.
linux_c_sources = glob.glob(os.path.join(cdf_linux_lib, '*.c'))
# libmain.c depends on windows, and should be removed from the source list.
linux_c_sources.remove(os.path.join(cdf_linux_lib, 'libmain.c'))

# Cython source files.
cython_sources = [
    os.path.join('cython_src', 'cdflib.pyx')
    ,]

# Figure out numpy's include directory.
from distutils.sysconfig import get_python_lib
site_pkg_dir = get_python_lib()
numpy_inc = os.path.join(site_pkg_dir, 'numpy', 'core', 'include')


#-------------------------------------------------------------------------------
# 3. Define modules for different platforms.
#-------------------------------------------------------------------------------
compile_args = ['-D_FILE_OFFSET_BITS=64', 
                '-D_LARGEFILE64_SOURCE',
                '-D_LARGEFILE_SOURCE', 
               ]
cdflib_module = PKG_NAME + '.cdflib'

import platform 
if platform.system() == 'Linux':
    sources       = cython_sources + linux_c_sources
    include_dirs  = [numpy_inc, cdf_linux_inc]
    extra_objects = []
    libraries     = ['m']
#     modules = Extension(cdflib_module,
#                         sources            = sources,
#                         extra_objects      = extra_objects,
#                         include_dirs       = include_dirs,
#                         extra_compile_args = compile_args, 
#                         libraries          = libraries,
#                        )
elif platform.system() == 'Darwin':
    sources       = cython_sources
    include_dirs  = [numpy_inc, cdf_macosx_inc] 
    extra_objects = [macosx_libcdf_a]
    libraries     = []
else:
    print platform.system(), ' is not supported yet. Abort.'
    import sys
    sys.exit(-1)

modules = Extension(cdflib_module,
                    sources            = sources,
                    extra_objects      = extra_objects,
                    include_dirs       = include_dirs,
                    extra_compile_args = compile_args, 
                    libraries          = libraries,
                   )

#-------------------------------------------------------------------------------
# 4. Call setup()
#-------------------------------------------------------------------------------
setup(name            = PKG_NAME,
      version         = VERSION,
      author          = AUTHOR,
      author_email    = JBT_EMAIL,
      packages        = PKG_LIST,
      url             = PROJECT_URL,
      cmdclass        = {'build_ext':build_ext},
      ext_modules     = [modules]
     )


